package com.example.shanguigu.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import static com.example.shanguigu.nio.Constants.BASE_DIR;

public class NIOFileChannel03 {

    public static void main(String[] args) {
        try (FileOutputStream fos=new FileOutputStream(BASE_DIR+"nio_copy.txt");
             FileChannel ofc= fos.getChannel();
                FileInputStream fis=new FileInputStream(BASE_DIR+"nio_out.txt");
             FileChannel ifc= fis.getChannel()){

            ByteBuffer buffer = ByteBuffer.allocate(1024);

            while (ifc.read(buffer)!=-1){
                buffer.flip();
                ofc.write(buffer);
                buffer.clear();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
